var pageNum = null;
var pageSize = 10;
let vm = new Vue({
    el: '.main-content',

    data: function () {
        return {
            pageInfo: {},
            area: '',
            sysArea: {},
            condition: {
                id: '',
                name: ''
            },

            setting: {
                data: {
                    simpleData: {
                        enable: true,
                        idKey: "id",
                        pIdKey: "parentId"
                    },

                }, callback: {
                    onClick: this.onClick,
                    onRename: this.onRename,
                    onRemove: this.onRemove
                },
                edit: {
                    enable: true,
                    removeTitle: "删除",
                    renameTitle: "修改"
                }
            },
            nodes: []
        }
    },
    methods: {

        getPage: function (pageNum, pageSize) {
            selectPage(pageNum, pageSize)
        },

        getPage2: function (pageNum = 1, pageSize = 5) {
            axios({
                url: `/guguanjia/area/findAll/${pageNum}/${pageSize}`,
                params: this.condition
            }).then(response => {
                vm.$data.pageInfo = response.data;
            }).catch(error => {
                layer.msg(error.message)
            })
        },


        selectAll: function () {
            //查询全部：  1.清空查询条件对象值  2.调用selectPage
            this.condition = {
                id: '',
                name: ''
            };
            selectPage(pageNum, pageSize)
        },

        selectName: function () {
            //清空id条件
            this.condition.id = '';
            this.getPage2()
        },


        toDemand: function (id) {
            layer.open({
                type: 2,
                title: false,
                area: ['70%', '70%'],
                content: '/guguanjia/area/toDemand',
                success: function (aa, index) {
                    var iframe = window['layui-layer-iframe' + index];
                    iframe.abcde(id)
                }
            })
        },

        toUpdate: function (id) {
            layer.open({
                type: 2,
                skin: 'layui-layer-demo', //样式类名
                title: '编辑',
                closeBtn: 0,
                anim: 2,
                area: ['80%', '80%'],
                shadeClose: true, //开启遮罩关闭
                content: '/guguanjia/area/toUpdate',
                success: function (aa, index) {
                    var iframe = window['layui-layer-iframe' + index];
                    iframe.abcde(id)
                },
                end: () => {
                    //体验不佳，刷新页面
                    // window.parent.location.reload();

                    //刷新页面，但ztree树需要手动刷新
                    this.getPage(pageNum, pageSize)
                }
            })
        },

        //删除
        doDelete: function (id) {
            layer.msg('是否删除', {
                time: 20000,
                btn: ['是', '否'],
                yes: () => {
                    axios({
                        url: `/guguanjia/area/deleteById/${id}`,
                        method: 'get',
                        data: id
                    }).then(response => {
                        layer.msg("删除成功");
                        //刷新页面，但ztree树需要手动刷新
                        this.getPage(pageNum, pageSize)
                    }).catch(error => {
                        layer.msg(error.message);
                    })
                },
                end: function () {
                    console.log("这是no？");
                    // window.parent.location.reload();
                    window.location.reload();
                    return false;
                }
            })
        },

        //文件上传操作
        upload: function (e) {
            let file = e.target.files[0];
            let formData = new FormData();
            formData.append("file", file);
            axios({
                url: "/guguanjia/area/upload",
                method: "GET",
                data: formData,
                headers: {
                    'content-type': 'multipart/form-data'
                }
            }).then(response => {
                console.log(123456);
                layer.msg(response.data);
            }).catch(error => {
                layer.msg(error.message)
                console.log(7891011);
            })
        },

        download: function () {
            location.href = "/guguanjia/area/Write";
            // location.href = 'mapper/area/download';
        },


        init: function () {
            //初始化nodes数据
            axios({
                url: "/guguanjia/area/findAll"
            }).then(response => {
                this.nodes = response.data;
                this.nodes[this.nodes.length] = {"id": 0, "name": "区域对象"};
                $.fn.zTree.init($("#treeMenu"), this.setting, this.nodes);
            }).catch(error => {
                layer.msg(error.message)
            })
        },

        onClick: function (event, treeId, treeNode) {
            if (treeNode.id === 0) {
                this.condition.id = null;
            } else {
                this.condition.id = treeNode.id;
                this.condition.name = '';
            }
            this.getPage2()
        },
        //ztree树编辑
        onRename: function (event, treeId, treeNode) {
            // console.log(treeNode)
            axios({
                url: "/guguanjia/area/updateById",
                method: "POST",
                data: treeNode
            }).then(resp => {
                if (treeNode.data) {
                    console.log(treeNode.name)
                    // layer.msg("修改成功")
                }
            }).catch(error => {
                layer.msg(error.message)
            });
            selectPage(pageNum, pageSize)
        },

        //ztree树删除
        onRemove: function (event, treeId, treeNode) {
            axios({
                url: `/guguanjia/area/deleteById/${treeNode.id}`,
                method: 'get',
                data: treeNode.id
            }).then(response => {
                layer.msg("删除成功");
            }).catch(error => {
                layer.msg(error.message);
            })
            //刷新页面，但ztree树需要手动刷新
            selectPage(pageNum, pageSize)
        },

    },

    created: function () {
        selectPage(1, 5);
        this.init();
    }

});

function selectPage(pageNum, pageSize) {
    this.pageNum = pageNum;
    this.pageSize = pageSize;
    axios({
        url: `/guguanjia/area/findAll/${pageNum}/${pageSize}`,
    }).then(response => {
        vm.$data.pageInfo = response.data;
    }).catch(error => {
        layer.msg(error.message)
    })
}